//
//  MessageViewController.swift
//  MT
//
//  Created by ooops on 2019/12/15.
//  Copyright © 2019 张强. All rights reserved.
//

import UIKit
import SnapKit
import JXCategoryView
import JXPagingView

class MessageViewController: BaseViewController {
    
    public var listViewDidScrollCallback: ((UIScrollView) -> ())?
    
    lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain).then {
            $0.register(MessageTableViewBaseCell.self, forCellReuseIdentifier: MessageTableViewBaseCell.reuseIdentity())
            $0.register(MessageRejectedCell.self, forCellReuseIdentifier: MessageRejectedCell.reuseIdentity())
            $0.register(MessageTimeStatusCell.self, forCellReuseIdentifier: MessageTimeStatusCell.reuseIdentity())
            $0.register(MessageConfrimCell.self, forCellReuseIdentifier: MessageConfrimCell.reuseIdentity())
            $0.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: CGFloat.leastNormalMagnitude))
            $0.backgroundColor = .clear
        }
        if #available(iOS 11.0, *) {
            tableView.contentInsetAdjustmentBehavior = UIScrollView.ContentInsetAdjustmentBehavior.never
        } else {
            self.automaticallyAdjustsScrollViewInsets = false
            tableView.translatesAutoresizingMaskIntoConstraints = false
        }
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        tableView.estimatedRowHeight = 70;
        tableView.estimatedSectionHeaderHeight = 0;
        tableView.estimatedSectionFooterHeight = 0;
        return tableView
    }()
    
    var model: MessageListModel?

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor(hex: 0x1D1A1A)
        
        let json = """
 {"data":[{"avator":"https://avatar.gitee.com/uploads/73/64673_yyyy.png!avatar200?1478868513","name":"IAN-伊恩啦啦啦","desc":"请求添加您为好友", "type":0, "timestamp":1576402503},{"avator":"https://avatar.gitee.com/uploads/73/64673_yyyy.png!avatar200?1478868513","name":"IAN-伊恩","desc":"hello~", "type":1, "timestamp":1576402503},{"avator":"https://avatar.gitee.com/uploads/73/64673_yyyy.png!avatar200?1478868513","name":"伊莎贝拉·王","desc":"伊莎贝拉·王", "type":2, "timestamp":1576404033},{"avator":"https://avatar.gitee.com/uploads/73/64673_yyyy.png!avatar200?1478868513","name":"Nink","desc":"对方拒绝了你的好友请求", "type":1, "timestamp":1576402503},{"avator":"https://avatar.gitee.com/uploads/73/64673_yyyy.png!avatar200?1478868513","name":"嘻嘻嘻嘻4","desc":"对方拒绝了你的好友请求", "type":2, "timestamp":1576402503}]}
"""
        
        self.model = MessageListModel.init(JSONString: json, context: nil)

        view.addSubview(tableView)
        
        tableView.snp.makeConstraints {
            $0.edges.equalToSuperview()
        }
        
        tableView.reloadData()
    }
}

extension MessageViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.model?.data?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//        let cell: MessageTimeStatusCell = tableView.dequeueReusableCell(withIdentifier: MessageTimeStatusCell.reuseIdentity(), for: indexPath) as! MessageTimeStatusCell
//
//        guard let cellModel: MListModel? = self.model?.data?[indexPath.row] else {
//            return cell
//        }
        
        let cell: MessageTimeStatusCell = tableView.dequeueReusableCell(withIdentifier: MessageTimeStatusCell.reuseIdentity(), for: indexPath) as! MessageTimeStatusCell
                    
        //            cell.cellModel = cellModel
        cell.textLabel?.text = "dddddddd"
        return cell
        
//        let cellModel: MListModel? = self.model?.data?[indexPath.row]
//
//        switch cellModel?.type {
//        case 0:
//            let cell: MessageConfrimCell = tableView.dequeueReusableCell(withIdentifier: MessageConfrimCell.reuseIdentity(), for: indexPath) as! MessageConfrimCell
//
//            cell.cellModel = cellModel
//
//            return cell
//
//        case 1:
//            let cell: MessageRejectedCell = tableView.dequeueReusableCell(withIdentifier: MessageRejectedCell.reuseIdentity(), for: indexPath) as! MessageRejectedCell
//
//            cell.cellModel = cellModel
//
//            return cell
//
//        case 2:
//            let cell: MessageTimeStatusCell = tableView.dequeueReusableCell(withIdentifier: MessageTimeStatusCell.reuseIdentity(), for: indexPath) as! MessageTimeStatusCell
//
//            cell.cellModel = cellModel
//
//            return cell
//
//        default:
//            let cell: MessageTimeStatusCell = tableView.dequeueReusableCell(withIdentifier: MessageTimeStatusCell.reuseIdentity(), for: indexPath) as! MessageTimeStatusCell
//
////            cell.cellModel = cellModel
//
//            return cell
//        }
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 70.0
    }
    
//    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
//        delete(indexPath)
//    }
//
    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
        return .delete
    }
    
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
        return false
    }
    
    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        let delete = UITableViewRowAction(style: .destructive, title: "删除") { action, index in
            self.model?.data?.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .fade)
        }
        delete.backgroundColor = UIColor.red
        return [delete]
    }
    
    func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        let deleteAction:UIContextualAction = UIContextualAction(style: .destructive, title: "") { (action, sourceView, completionHandler) in
            completionHandler(true)
        }
        deleteAction.image = UIImage(named: "icon_trash")
        deleteAction.backgroundColor = UIColor(hex: 0xFFC000)
        let actions:[UIContextualAction] = [deleteAction]
        let action:UISwipeActionsConfiguration = UISwipeActionsConfiguration(actions: actions)
        action.performsFirstActionWithFullSwipe = false
        return action
    }
    
//    func delete(_ indexPath: IndexPath) {
//        self.model?.data?.remove(at: indexPath.row)
//        self.tableView.reloadData()
//    }
}

extension MessageViewController: JXPagingViewListViewDelegate {
    func listViewDidScrollCallback(callback: @escaping (UIScrollView) -> ()) {
        listViewDidScrollCallback = callback
    }


    func listView() -> UIView {
        return self.view
    }

    func listScrollView() -> UIScrollView {
        return self.tableView
    }
}
